import store from "@/store"

// 腾讯地址逆解析
export function txLocation(location = {}) {
  return new Promise(async (resolve, reject) => {
    const { mapSecret } = store.getters
    if (!mapSecret) {
      return reject(new Error("地图密钥未配置"))
    }
    try {
      const data = {
        key: mapSecret,
        location: `${location.latitude},${location.longitude}`,
        // #ifdef H5
        output: "jsonp",
        callback: "callback"
        // #endif
      }
      let res = {}
      // #ifdef H5
      res = await uni.$request.jsonp("https://apis.map.qq.com/ws/geocoder/v1", data)
      // #endif
      // #ifndef H5
      res = await uni.$request.get("https://apis.map.qq.com/ws/geocoder/v1", data)
      // #endif
      if (res?.status === 0) {
        resolve({
          cityCode: res.result.ad_info.city_code?.slice(-6),
          cityName: res.result.ad_info.city,
          areaName: res.result.ad_info.district,
          provinceName: res.result.ad_info.province,
          address: res.result.address,
          latitude: location.latitude,
          longitude: location.longitude
        })
      } else {
        return reject(res)
      }
    } catch (error) {
      return reject(error)
    }
  })
}

// 腾讯IP地址解析
export function txIpLocation() {
  return new Promise(async (resolve, reject) => {
    const { mapSecret } = store.getters
    if (!mapSecret) {
      return reject(new Error("地图密钥未配置"))
    }
    try {
      const data = {
        key: mapSecret,
        // #ifdef H5
        output: "jsonp",
        callback: "callback"
        // #endif
      }
      let res = {}
      // #ifdef H5
      res = await uni.$request.jsonp("https://apis.map.qq.com/ws/location/v1/ip", data)
      // #endif
      // #ifndef H5
      res = await uni.$request.get("https://apis.map.qq.com/ws/location/v1/ip", data)
      // #endif
      if (res?.status === 0) {
        const location = await txLocation({ latitude: res.result.location.lat, longitude: res.result.location.lng })
        resolve(location)
      } else {
        return reject(res)
      }
    } catch (error) {
      return reject(error)
    }
  })
}

// 获取定位信息
export function getLocation() {
  return new Promise(async (resolve, reject) => {
    // 超时容错处理
    const timer = setTimeout(async () => {
      console.log("GPS定位超时")
      console.log("开始IP定位")
      const res = await txIpLocation()
      console.log("IP定位成功", res)
      resolve(res)
    }, 3000)
    console.log("开始GPS定位")
    uni.getLocation({
      type: "gcj02",
      success: async (location) => {
        try {
          clearTimeout(timer)
          console.log("GPS定位成功")
          console.log("开始定位解析")
          const res = await txLocation(location)
          console.log("定位解析成功", res)
          resolve(res)
        } catch (error) {
          reject(error)
        }
      },
      fail: async () => {
        try {
          clearTimeout(timer)
          console.log("GPS定位失败")
          console.log("开始IP定位")
          const res = await txIpLocation()
          console.log("IP定位成功", res)
          resolve(res)
        } catch (error) {
          reject(error)
        }
      }
    })
  })
}
